package QRCG;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import com.sun.mail.smtp.*;

public class Mailer {
  private static final String HOST_NAME = "smtp.gmail.com";
  private static final String USER_NAME = "qr.information";
  private static final String FULL_MAIL_ADDRESS = USER_NAME + "@gmail.com";
  private static final String PASSWORD  = "qr123456";

  public static void send(String subject, String body) throws MessagingException {
    Mailer.send(FULL_MAIL_ADDRESS, FULL_MAIL_ADDRESS, subject, body);
  }

  public static void send(String from, String subject, String body) throws MessagingException {
    Mailer.send(from, FULL_MAIL_ADDRESS, subject, body);
  }

  public static void send(String from, String to, String subject, String body) throws MessagingException {
    Properties props = System.getProperties();
    props.put("mail.smtps.auth", "true"); // Use secure SMTP, since Gmail requires so.
    props.put("mail.smtps.host", HOST_NAME);
    props.put("mail.smtps.password", PASSWORD);

    // Obtain a unique session.
    Session session = Session.getInstance(props, null);
    //session.setDebug(true)
    Message msg = new MimeMessage(session);
    if (from != null)
      msg.setFrom(new InternetAddress(from));

    msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
    msg.setSubject(subject);
    msg.setText(body);

    SMTPTransport t = (SMTPTransport) session.getTransport("smtps");
    t.connect("smtp.gmail.com", FULL_MAIL_ADDRESS, PASSWORD);
    t.sendMessage(msg, msg.getAllRecipients());
    System.out.println("JavaMail -- Gmail Server Response: " + t.getLastServerResponse());
    t.close();
  }


  /**
   * Checks the validity of an email address.
   *
   * The checking is mostly carried through {@link javax.mail.internet.InternetAddress}. There is
   * an additional check that invalidates emails such as "john", without any domain name or "@"
   * character. Though such strings are technically valid, they are almost never intended to be
   * accepted as valid in practice.
   * @param mail The mail to be tested.
   * @return Whether the given mail address is valid.
   */
  public static boolean isValidMailAddress(String mail) {
    if (mail == null) return false;
    try {
      InternetAddress ia = new InternetAddress(mail, true);
      return hasNameAndDomain(mail);
    } catch (AddressException ae) {
      return false;
    }
  }

  public static String getFullMailAddress() {
    return FULL_MAIL_ADDRESS;
  }
  
  /**
   * @param mail The email string to be tested.
   * @return Whether the string has two non-empty (i.e. some character without whitespace) tokens
   *     separated by "@".
   */
  private static boolean hasNameAndDomain(String mail) {
    String[] tokens = mail.split("@");
    return tokens.length == 2 &&
           Util.hasTextContent(tokens[0]) &&
           Util.hasTextContent(tokens[1]);
  }
}
